package com.mmy.controller.car;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.car.TCarPlan;
import com.mmy.domain.car.VCarBase;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.impl.TCarBaseServiceImpl;
import com.mmy.service.car.impl.TCarPlanServiceImpl;

/**
 * 类的介绍：车公司信息
 *
 * @author shangzl
 * @date 创建日期：2020-08-11
 * @version 1.0
 */
@Controller
public class CarPlanController {

	@Autowired
	TCarPlanServiceImpl tCarPlanServiceImpl;
	@Autowired
	TCarBaseServiceImpl tCarBaseServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-11
	 */
	@RequestMapping(value = "/carplan")
	public String ToCarPlan(Model model) {
		try {

			List<TCarPlan> lstCarPlan = tCarPlanServiceImpl.findAll();
			model.addAttribute("carplanlist", lstCarPlan);
			List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
			model.addAttribute("carbaselist", lstCarBase);

			return "webbackground/car/carplan";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createcarplan")
	public String ToCreateCarPlan(Model model) {
		List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
		model.addAttribute("carbaselist", lstCarBase);
		return "webbackground/car/createcarplan";
	}

	/**
	 * 函数功能说明： 新建车计划信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-06
	 */
	@RequestMapping(value = "/subcreatecarplan")
	public String CreateCarPlan(TCarPlan tCarPlan, String startTime, String endTime, HttpSession session)
			throws Exception {

		tCarPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tCarPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));

		try {
			// 获取最大值
			tCarPlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_CAR_PLAN));

			tCarPlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tCarPlan.setCreator(tSysUser.getUserName());
			tCarPlanServiceImpl.save(tCarPlan.getPlanID());
			tCarPlanServiceImpl.save(tCarPlan);
			return "redirect:carplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除车计划信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@PostMapping("/delcarplan")
	@ResponseBody
	public String DelCarPlan(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();

		for (String str : checklist) {
			lstPlanID.add(Integer.parseInt(str));
		}

		tCarPlanServiceImpl.delCarPlan(lstPlanID);

		return "webbackground/car/carplan";
	}

	/**
	 * 函数功能说明： 修改车计划信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@RequestMapping(value = "/enterupdatecarplan")
	public String EnterUpdateCarPlan(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		TCarPlan tCarPlan = tCarPlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("tCarPlan", tCarPlan);
		return "webbackground/car/updatecarplan";

	}

	@RequestMapping(value = "/updatecarplan")
	public String UpdateCarPlan(TCarPlan tCarPlan, HttpSession session) throws Exception {
		try {

//			 定义一个新的实体类对象 
			TCarPlan dtCarPlan = tCarPlanServiceImpl.findByPlanID(tCarPlan.getPlanID());
			dtCarPlan.setFloor(tCarPlan.getFloor());
			dtCarPlan.setPeer(tCarPlan.getPeer());
			dtCarPlan.setPrice(tCarPlan.getPrice());
			dtCarPlan.setMemo(tCarPlan.getMemo());
			dtCarPlan.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarPlan.setReviser(tSysUser.getUserName());
			tCarPlanServiceImpl.save(dtCarPlan);
			return "redirect:carplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询车计划信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */
	@RequestMapping(value = "/searchcarplan")
	public String FindCarPlan(HttpServletRequest request, Model model) {

		String strCarID = request.getParameter("CarID");
		if (strCarID == "") {
			List<TCarPlan> lstCarPlan = tCarPlanServiceImpl.findAll();
			model.addAttribute("carplanlist", lstCarPlan);
			List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
			model.addAttribute("carbaselist", lstCarBase);
			return "webbackground/car/carplan";
		} else {
			List<TCarPlan> lstCarPlan = tCarPlanServiceImpl.findByCarID(strCarID);
			model.addAttribute("carplanlist", lstCarPlan);
			List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
			model.addAttribute("carbaselist", lstCarBase);
			return "webbackground/car/carplan";

		}

	}

}
